﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace TVPlayer
{
    public partial class ConfigWindow : Form
    {
        private Configuration config;

        public Configuration Config
        {
            get { return config; }
            set { config = value; }
        }

        public ConfigWindow()
        {
            InitializeComponent();
        }

        private void LoadData()
        {
            checkBoxConnectOnStartup.Checked = config.ConnectOnStartup;
            textBoxMediaPortalServer.Text = config.MediaPortalServer;
            textBoxVLCPath.Text = config.VlcLocation;
            comboBoxVLCInterlaceMode.SelectedItem = string.IsNullOrEmpty(config.VlcDeinterlace) ? comboBoxVLCInterlaceMode.Items[0] : config.VlcDeinterlace;
            checkBoxCloseEPGOnChannelChange.Checked = config.CloseEPGOnChannelChange;
            checkBoxTuneLastChannelOnStartup.Checked = config.TuneLastChannelOnStartUp;

            switch (config.InformationPanelDisplayMode)
            {
                case InformationPanelDisplayModes.AlwaysHide:
                    comboBoxInformationPanel.SelectedItem = "Always hide";
                    break;
                case InformationPanelDisplayModes.AlwaysShow:
                    comboBoxInformationPanel.SelectedItem = "Always show";
                    break;
                case InformationPanelDisplayModes.ShowOnchannelChange:
                    comboBoxInformationPanel.SelectedItem = "Show on channel change (10s)";
                    break;
                default:
                    comboBoxInformationPanel.SelectedItem = comboBoxInformationPanel.Items[0];
                    break;
            }

            listBoxVLCOptions.Items.Clear();
            foreach (string item in config.VLCOptions)
            {
                listBoxVLCOptions.Items.Add(item);
            }
        }

        private void SaveData()
        {
            config.ConnectOnStartup = checkBoxConnectOnStartup.Checked;
            config.MediaPortalServer = textBoxMediaPortalServer.Text;
            config.VlcLocation = textBoxVLCPath.Text;
            config.VlcDeinterlace = comboBoxVLCInterlaceMode.SelectedItem.ToString();
            config.CloseEPGOnChannelChange = checkBoxCloseEPGOnChannelChange.Checked;
            config.TuneLastChannelOnStartUp = checkBoxTuneLastChannelOnStartup.Checked;

            if (comboBoxInformationPanel.SelectedItem.ToString() == "Always hide")
            {
                config.InformationPanelDisplayMode = InformationPanelDisplayModes.AlwaysHide;
            }
            else if (comboBoxInformationPanel.SelectedItem.ToString() == "Always show")
            {
                config.InformationPanelDisplayMode = InformationPanelDisplayModes.AlwaysShow;
            }
            else if (comboBoxInformationPanel.SelectedItem.ToString() == "Show on channel change (10s)")
            {
                config.InformationPanelDisplayMode = InformationPanelDisplayModes.ShowOnchannelChange;
            }

            List<string> vlcOptions = new List<string>();
            foreach (object item in listBoxVLCOptions.Items)
            {
                vlcOptions.Add(item.ToString());
            }
            config.VLCOptions = vlcOptions;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            SaveData();
            this.Close();
        }

        private void ConfigWindow_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void buttonLocateVLC_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                textBoxVLCPath.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void linkLabelMediaPortal_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("http://www.team-mediaportal.com");
        }

        private void linkLabelVLC_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("http://www.videolan.org/vlc/");
        }

        private void buttonAddVLCOption_Click(object sender, EventArgs e)
        {
            InputDialog dlgInput = new InputDialog();
            dlgInput.InformationText = "Enter vlc option text";
            if (dlgInput.ShowDialog() == DialogResult.OK && !String.IsNullOrEmpty(dlgInput.InputValue))
            {
                listBoxVLCOptions.Items.Add(dlgInput.InputValue);
            }
        }

        private void buttonRemoveVLCOption_Click(object sender, EventArgs e)
        {
            if (listBoxVLCOptions.SelectedItem != null)
                listBoxVLCOptions.Items.Remove(listBoxVLCOptions.SelectedItem);
        }

        private void listBoxVLCOptions_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (listBoxVLCOptions.SelectedItem == null)
                return;

            int idx = listBoxVLCOptions.SelectedIndex;
            InputDialog dlgInput = new InputDialog();
            dlgInput.InformationText = "Enter vlc option text";
            dlgInput.InputValue = listBoxVLCOptions.SelectedItem.ToString();

            if (dlgInput.ShowDialog() == DialogResult.OK && !String.IsNullOrEmpty(dlgInput.InputValue))
            {
                listBoxVLCOptions.Items[idx] = dlgInput.InputValue;
            }
        }

        private void checkBoxConnectOnStartup_CheckedChanged(object sender, EventArgs e)
        {
            checkBoxTuneLastChannelOnStartup.Enabled = checkBoxConnectOnStartup.Checked;
        }
    }
}
